#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>

#define PLUGIN_NAME                   "l4d2_Chargerr_damage_scopee"    //这里写插件名字
#define PLUGIN_AUTHOR                 "豆瓣酱な"                         //作者
#define PLUGIN_DESCRIPTION            "牛牛冲锋触发秒杀范围"   //介绍
#define PLUGIN_VERSION                "1.0"
#define PLUGIN_URL                    "https://gitee.com/sweet-and-sour-bombs"  //网站

public Plugin myinfo =
{
    name        = PLUGIN_NAME,
    author      = PLUGIN_AUTHOR,
    description = PLUGIN_DESCRIPTION,
    version     = PLUGIN_VERSION,
    url         = PLUGIN_URL
}

Handle hCvar_Trigger_charger;
float Trigger_dis_charger;

int  g_ChargerFanWeiKill;
ConVar hCvar_ChargerFanWeiKill;

public void OnPluginStart()
{
	RegAdminCmd("sm_ms", Command_pounceSwitch, ADMFLAG_KICK, "Charger冲锋秒杀范围指令总开关");
	RegAdminCmd("sm_chargerfwms", Command_pounceSwitch, ADMFLAG_KICK, "Charger冲锋秒杀范围指令开关");
	hCvar_ChargerFanWeiKill = CreateConVar("l4d2_charger_damassge_kille", "1", "是否启用Charger冲锋秒杀范围插件", FCVAR_NOTIFY, true, 0.0, true, 1.0);
	HookConVarChange(hCvar_ChargerFanWeiKill, eConvarChanged);	


	HookEvent("charger_charge_start", Event_Charge_Start, EventHookMode_Pre);   //充电器_充电_开始事件.
	HookEvent("charger_carry_end", Event_Charge_Start, EventHookMode_Pre);   //充电器充电结束事件.
	HookEvent("charger_carry_start", Event_Charge_Start, EventHookMode_Pre);   //充电器携带开始事件.
	HookEvent("charger_carry_end", Event_Charge_Start, EventHookMode_Pre);   //充电器携带结束事件.
	HookEvent("charger_pummel_start", Event_Charge_Start, EventHookMode_Pre);   //充电器抓住幸存者摔击幸存者事件.
	HookEvent("charger_pummel_end", Event_Charge_Start, EventHookMode_Pre);   //充电器抓住幸存者摔击幸存者结束事件.
	
	hCvar_Trigger_charger = CreateConVar("l4d2_hCxvar_charger_diss", "300.0", "玩家与Charger多少距离之内，玩家被秒杀?", FCVAR_NOTIFY);
	HookConVarChange(hCvar_Trigger_charger, eConvarChanged);
	
	//AutoExecConfig(true, "l4d2_Charger_damage_scoxpe");
}

public void OnMapStart()
{
	CvarsChanged();
}

public void eConvarChanged(Handle hCvar, const char[] sOldVal, const char[] sNewVal)
{
	CvarsChanged();
}

void CvarsChanged()
{
	Trigger_dis_charger = GetConVarFloat(hCvar_Trigger_charger);
	g_ChargerFanWeiKill = hCvar_ChargerFanWeiKill.IntValue;	
}

public Action Command_pounceSwitch(int client, int args)
{
	if (g_ChargerFanWeiKill)
	{
		hCvar_ChargerFanWeiKill.IntValue = 0;
		PrintToChat(client, "\x04[提示]\x03Charger\x05范围秒杀插件已\x03关闭\x05.");
	}
	else
	{
		hCvar_ChargerFanWeiKill.IntValue = 1;
		PrintToChat(client, "\x04[提示]\x03Charger\x05范围秒杀插件已\x03开启\x05.");
	}
	return Plugin_Handled;
}

void Event_Charge_Start(Event event, const char[] name, bool dontBroadcast)
{
	int userid = GetEventInt(event, "userid");
	int Client = GetClientOfUserId(userid);
	if (IsInfected(Client)) 
	{
		CreateTimer(0.1, nexthealing_inf, userid, TIMER_FLAG_NO_MAPCHANGE);
			
	}
}

public Action nexthealing_inf(Handle timer, any client)
{
	client = GetClientOfUserId(client);
	if(IsCharger(client) && IsPlayerAlive(client) && g_ChargerFanWeiKill == 1)
	{
		//PrintToChatAll("\x04[提示]\x03Charger\x05非常愤怒，发动了\x03秒杀\x05！");

		float pos[3];
		float entpos[3];
		float distance;

		GetEntPropVector(client, Prop_Send, "m_vecOrigin", pos);
		for (int i = 1; i <= MaxClients; i++)
		{
			if (!IsSurvivor(i) || !IsPlayerAlive(i))
				continue;
			GetEntPropVector(i, Prop_Send, "m_vecOrigin", entpos);
			distance = GetVectorDistance(pos, entpos);
			if (distance<=Trigger_dis_charger)
			{
				ForcePlayerSuicide(i);
				PrintToChatAll("\x04[提示]\x03Charger%s\x05杀死了\x03%N\x05.", GetPlayerName(client, true), i);
			}
		}
	}
	return Plugin_Continue;
}

bool IsSurvivor(int client)
{
	return client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == 2;
}

bool IsInfected(int client)
{
	return client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == 3;
}

bool IsCharger(int client)  
{
	if (!IsInfected(client))
		return false;

	if (GetEntProp(client, Prop_Send, "m_zombieClass") != 6)
		return false;

	return true;
}

char[] GetPlayerName(int client, bool bPromptType)
{
	char sName[32];
	if (!IsFakeClient(client) && IsPlayerAlive(client) && GetClientTeam(client) == 3 && GetEntProp(client, Prop_Send, "m_zombieClass"))
		FormatEx(sName, sizeof(sName), "%s%N", !bPromptType ? "" : "\x04", client);
	else
	{
		GetClientName(client, sName, sizeof(sName));
		SplitString(sName, "", sName, sizeof(sName));
	}
	return sName;
}



